package com.botu.sentineldemo1.demo;
import	java.util.ArrayList;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: hanzeyao
 * @Date: 2019/12/6 20:18
 * @Description: 响应时间
 */
public class RtDegradeDemo2 {

    private static final String Key ="abc";

    public static void main(String[] args) {
        while(true) {
            test();
        }
    }

    private static void test(){
        initDegradeRule();
        Entry entry =null;
        try {
            entry = SphU.entry(Key);
            System.out.println("======pass=====");
            //模拟响应超时
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (BlockException e) {
            System.out.println("-----block-----");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            if(entry != null){
                entry.exit();
            }
        }
    }

    private static void initDegradeRule(){
        List<DegradeRule> rules = new ArrayList<>();
        DegradeRule rule = new DegradeRule();
        rule.setResource(Key);
        //响应时间10ms
        rule.setCount(10);
        rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
        rule.setTimeWindow(10);
        rules.add(rule);
        DegradeRuleManager.loadRules(rules);
    }
}
